package com.tgpms.web.element.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.VformBeanUtil;
import com.tgpms.web.element.entity.MaterialExamination;
import com.tgpms.web.element.entity.MaterialExaminationNo;
import com.tgpms.web.element.service.CommodityAppService;

import lombok.extern.slf4j.Slf4j;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
@Slf4j
public class CommodityAppServiceImpl implements CommodityAppService{
	
	@Autowired
    private VformInterface vformInterface;

	//物资验收交验单提交
	@Override
	public Result<Object> checkListFunction(MaterialExamination en) {
		//D4190屏幕提交
        JSONObject keyjson = new JSONObject();
        keyjson.put("EXAMINATION", en.getExamination());
        JSONObject pajson = new JSONObject();
        pajson = VformBeanUtil.entityToJsonStringByUpdate(en);
        pajson.remove("EXAMINATION");
        pajson.remove("BATCHLIST");
        pajson.remove("MATERIAL_QTY");
        pajson.remove("GW");
        pajson.remove("EXAMINATION_DATE");
        //vform同步更新
        System.out.println("物资验收交验单提交更新的参数为:"+pajson.toJSONString());
        Result result = vformInterface.update("D4190", keyjson.toJSONString(),pajson.toJSONString(),"Y","Y",en.getDataOwner());
        log.info("物资验收交验单提交vform更新返回的结果为：{}"+result);
        if(result==null || !result.getSuccess()) {
            return Result.ERROR(ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
        }
        if(result.getData().toString().equals("{\"message\":\"login\"}")) {
        	return Result.ERROR(ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
        }
        String resData = result.getData().toString();
        JSONObject reJson = JSONObject.parseObject(resData);
        if(reJson.get("status").toString().equals("error")) {
        	return Result.ERROR(reJson.get("validateError").toString());
        }else {
        	return Result.OK(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
        }
	}

	//增加批次
	@Override
	public Result<Object> addMaterialExaminationNo(MaterialExaminationNo en) {
		JSONObject enjson = new JSONObject();
		en.setNo(1);
		enjson = VformBeanUtil.entityToJsonStringByUpdate(en);
		Result result = vformInterface.insert("D4191", enjson);
        log.info("增加批次vform返回的结果为：{}"+result);
        if(result==null || !result.getSuccess()) {
            return Result.ERROR(ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
        }
        if(result.getData().toString().equals("{\"message\":\"login\"}")) {
        	return Result.ERROR(ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
        }
        String resData = result.getData().toString();
        JSONObject reJson = JSONObject.parseObject(resData);
        if(reJson.get("status").toString().equals("error")) {
        	return Result.ERROR(reJson.get("validateError").toString());
        }else {
        	return Result.OK(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
        }
	}

	//更新批次
	@Override
	public Result<Object> upMaterialExaminationNo(MaterialExaminationNo en) {
		JSONObject keyjson = new JSONObject();
        keyjson.put("EXAMINATION", en.getExamination());
        keyjson.put("MATERIAL_NO", en.getMaterialNo());
        keyjson.put("NO", "1");
        JSONObject pajson = new JSONObject();
        pajson = VformBeanUtil.entityToJsonStringByUpdate(en);
        pajson.remove("EXAMINATION");
        pajson.remove("MATERIAL_NO");
        pajson.remove("NO");
        //vform同步更新
        Result result = vformInterface.update("D4191", keyjson.toJSONString(),pajson.toJSONString(),"N","N",null);
        log.info("更新批次vform返回的结果为：{}"+result);
        if(result==null || !result.getSuccess()) {
            return Result.ERROR(ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
        }
        if(result.getData().toString().equals("{\"message\":\"login\"}")) {
        	return Result.ERROR(ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
        }
        String resData = result.getData().toString();
        JSONObject reJson = JSONObject.parseObject(resData);
        if(reJson.get("status").toString().equals("error")) {
        	return Result.ERROR(reJson.get("validateError").toString());
        }else {
        	return Result.OK(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
        }
	}

	//删除批次
	@Override
	public Result<Object> delMaterialExaminationNo(MaterialExaminationNo en) {
		JSONObject keyjson = new JSONObject();
        keyjson.put("EXAMINATION", en.getExamination());
        keyjson.put("MATERIAL_NO", en.getMaterialNo());
        keyjson.put("NO", "1");
        Result result = vformInterface.delete("D4191", keyjson.toJSONString());
        log.info("删除批次vform返回的结果为：{}"+result);
        if(result==null || !result.getSuccess()) {
            return Result.ERROR(ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
        }
        if(result.getData().toString().equals("{\"message\":\"login\"}")) {
        	return Result.ERROR(ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
        }
        String resData = result.getData().toString();
        JSONObject reJson = JSONObject.parseObject(resData);
        if(reJson.get("status").toString().equals("error")) {
        	return Result.ERROR(reJson.get("validateError").toString());
        }else {
        	return Result.OK(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
        }
	}

}
